package com.masterclient.app.modules.goods.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.base.FragmentHostActivity;
import com.eoner.baselibrary.bean.goods.ChildProductMessage;
import com.eoner.baselibrary.bean.goods.CommodityAttribute;
import com.eoner.baselibrary.bean.goods.CommodityAttributeOption;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.R;
import com.masterclient.app.widget.FlowLayout;
import com.masterclient.app.widget.KeyboardStatusDetector;
import com.masterclient.app.widget.PriceIntegralLayout;

import java.util.ArrayList;
import java.util.List;

import static com.eoner.baselibrary.utils.ScreenUtils.dp2px;

public class SkuSelectDialog extends Dialog implements View.OnClickListener {

    private Activity context;
    private List<FlowLayout> flowLayoutList;
    private CommodityDetailData data;
    private LinearLayout sku_container_layout;

    private PriceIntegralLayout price_ll;
    private TextView earn_txt;
    private TextView tv_vip_price;
    private LinearLayout layout_vip_price;
    private TextView line_price_txt;
//    private TextView integral_line_price;
    private TextView tv_double_earn;
    private TextView stock_txt;
    private TextView sku_title;
    private TextView sku_selected_txt;
    private ImageView iv_goods;
    private View close_layout;
    private View ll_double_earn;
    TextView left_button,right_button;

    TextView coupon_price;
    TextView coupon_price_desc;
    View coupon_price_layout;

    private TextView tv_activity_tag; //赠送价

    private int bottom_btn_type;//0展示加入购物车按钮，1展示立即购买按钮，2两个都展示

    public SkuSelectDialog(@NonNull Activity context, CommodityDetailData data,int bottom_btn_type) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.data = data;
        this.bottom_btn_type = bottom_btn_type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sku_select_dialog);
        setCanceledOnTouchOutside(true);
        options_selected = new ArrayList<>();
        flowLayoutList = new ArrayList<>();
        tv_activity_tag = findViewById(R.id.tv_activity_tag);
        sku_container_layout = (LinearLayout) findViewById(R.id.sku_container_layout);
        price_ll = (PriceIntegralLayout) findViewById(R.id.ll_price);
        earn_txt = (TextView) findViewById(R.id.earn_txt);
        tv_vip_price = (TextView) findViewById(R.id.tv_vip_price);
        layout_vip_price = (LinearLayout) findViewById(R.id.layout_vip_price);
        line_price_txt = (TextView) findViewById(R.id.line_price_txt);
//        integral_line_price = (TextView) findViewById(R.id.integral_line_price);
        tv_double_earn = (TextView) findViewById(R.id.tv_double_earn);
        stock_txt = (TextView) findViewById(R.id.stock_txt);
        sku_title = (TextView) findViewById(R.id.sku_title);
        sku_selected_txt = (TextView) findViewById(R.id.sku_selected_txt);
        iv_goods = (ImageView) findViewById(R.id.iv_goods);
        close_layout = findViewById(R.id.close_layout);
        ll_double_earn = findViewById(R.id.ll_double_earn);
        left_button = findViewById(R.id.left_button);
        right_button = findViewById(R.id.right_button);

        //券后价
        coupon_price = findViewById(R.id.coupon_price);
        coupon_price_desc = findViewById(R.id.coupon_price_desc);
        coupon_price_layout = findViewById(R.id.coupon_price_layout);

//        Utils.setDin(price_txt, context);
        Utils.setDin(earn_txt, context);
        Utils.setDin(line_price_txt, context);
//        Utils.setDin(integral_line_price, context);
        Utils.setDin(coupon_price, context);
        findViewById(R.id.scrollView).getLayoutParams().height = (int) (ScreenUtils.getScreenHeight() * 2 / 5f);
        if(!TextUtils.isEmpty(data.getLimit_num())){
            limitNum = Integer.parseInt(data.getLimit_num());
        }
        init_default_options_selected();
        display_price_layout();
        initViews();
        add_listener();

    }

    private void init_default_options_selected() {
        List<ChildProductMessage> childProductMessages = data.getChild_products();
        List<CommodityAttribute> attributes = data.getAttributes();
        if(childProductMessages != null && childProductMessages.size() == 1){
            ChildProductMessage childProductMessage = childProductMessages.get(0);
            String [] sku_code_array = childProductMessage.getSku_code().split("_");
            for(String sku_code: sku_code_array){
                for(CommodityAttribute attribute : attributes){
                    List<CommodityAttributeOption> options = attribute.getOptions();
                    for(CommodityAttributeOption option : options){
                        if(sku_code.equals(option.getOption_id())){
                            options_selected.add(option);
                        }
                    }
                }
            }
        }else{
            try{
                //规格只有一个时候 默认选中
                for(CommodityAttribute commodityAttribute : attributes){
                    if(commodityAttribute.getOptions().size() == 1){
                        options_selected.add(commodityAttribute.getOptions().get(0));
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void add_listener() {
        close_layout.setOnClickListener(this);
        iv_goods.setOnClickListener(this);
        left_button.setOnClickListener(this);
        right_button.setOnClickListener(this);
    }

    List<CommodityAttributeOption> options_selected;
    private ImageView iv_reduce, iv_plus;
    RelativeLayout rl_reduce, rl_plus;
    EditText et_num;
    TextView tv_limit,tv_limit_quantity;
    public int buyNum = 1;
    public int limitNum = 0;//默认0 代表不限购
    private String beforeStr;
    private void initViews() {
        sku_container_layout.removeAllViews();
        List<CommodityAttribute> attributes = data.getAttributes();
        for (int i = 0; i < attributes.size(); i++) {
            CommodityAttribute attribute = attributes.get(i);
            View view = View.inflate(context, R.layout.view_sku_condition_selector, null);
            TextView tv_label = (TextView) view.findViewById(R.id.tv_label);
            FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flowLayout);
            flowLayoutList.add(flowLayout);
            tv_label.setText(attribute.getLabel());
            initSingleAttribute(attribute, flowLayout);
            sku_container_layout.addView(view);
        }
        View line = new View(context);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(0.5f));
        param.setMargins(0, ScreenUtils.dp2px(6), 0, 0);
        line.setLayoutParams(param);
        line.setBackgroundResource(R.color.gray_line);
        sku_container_layout.addView(line);

        //选择数量ui加进来
        View view_num = View.inflate(context, R.layout.view_sku_goods_num, null);
        iv_plus = (ImageView) view_num.findViewById(R.id.iv_plus);
        iv_reduce = (ImageView) view_num.findViewById(R.id.iv_reduce);
        rl_reduce = (RelativeLayout) view_num.findViewById(R.id.rl_reduce);
        rl_reduce.setOnClickListener(this);
        rl_plus = (RelativeLayout) view_num.findViewById(R.id.rl_plus);
        rl_plus.setOnClickListener(this);
        et_num = (EditText) view_num.findViewById(R.id.et_num);
        Utils.setDin(et_num, context);
        tv_limit = (TextView) view_num.findViewById(R.id.tv_limit);
        tv_limit_quantity = (TextView) view_num.findViewById(R.id.tv_limit_quantity);

        et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeStr = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().equals(beforeStr)) {
                    return;
                }
                if (!TextUtils.isEmpty(s)) {
                    Integer num = Integer.valueOf(s.toString());
                    buyNum = num;
                    changeReducePlus(num);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        et_num.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    buyNum = Integer.valueOf(et_num.getText().toString().trim());
                }
            }
        });
        sku_container_layout.addView(view_num);

        //所有商品库存都为0时，不可编辑数量
        if (0 == Long.valueOf(stock)) {
            et_num.setEnabled(false);
            iv_reduce.setImageResource(R.mipmap.sku_dialog_less_unclick_icon);
            iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
        }else if("3".equals(data.getProduct_type())){
            //0元试用的购买数量ui
//            rl_reduce.setEnabled(false);
//            rl_plus.setEnabled(false);
//            et_num.setEnabled(false);
//            iv_reduce.setImageResource(R.mipmap.sku_dialog_less_unclick_icon);
//            iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
        }else if("4".equals(data.getProduct_type())){
            //礼包的购买数量ui
//            rl_reduce.setEnabled(false);
//            rl_plus.setEnabled(false);
//            et_num.setEnabled(false);
//            iv_reduce.setImageResource(R.mipmap.sku_dialog_less_unclick_icon);
//            iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
        }
        if(limitNum > 0){
            tv_limit.setVisibility(View.VISIBLE);
            if("3".equals(data.getProduct_type())){
                tv_limit.setText("限领" + limitNum + "件");
            }else{
                tv_limit.setText("限购" + limitNum + "件");
            }
            if (limitNum <= 1) {
                rl_reduce.setEnabled(false);
                rl_plus.setEnabled(false);
                et_num.setEnabled(false);
                iv_reduce.setImageResource(R.mipmap.sku_dialog_less_unclick_icon);
                iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
            }
        }

        new KeyboardStatusDetector().registerActivity(context).setmVisibilityListener(new KeyboardStatusDetector.KeyboardVisibilityListener() {
            @Override
            public void onVisibilityChanged(boolean keyboardVisible) {
                if (!keyboardVisible) {
                    if (TextUtils.isEmpty(et_num.getText().toString().trim())) {
                        et_num.setText("1");
                        buyNum = 1;
                    }
                }
            }
        });

        if (data.getActivity_type().equals("3")) {
            Utils.setDin(tv_activity_tag, context);
            tv_activity_tag.setVisibility(View.VISIBLE);
        }
        //展示底部的按钮及其样式
        display_bottom_btn();
    }

    private void display_bottom_btn() {
        if(Long.valueOf(stock) < 1){
            left_button.setVisibility(View.GONE);
            right_button.setVisibility(View.VISIBLE);
            right_button.setText("已售罄");
            right_button.setBackgroundResource(R.drawable.detail_sellout_bkg);
            right_button.setTextColor(Color.parseColor("#66000000"));
            return;
        }

        if (data.getActivity_type().equals("3")) {
            left_button.setVisibility(View.GONE);
            right_button.setVisibility(View.VISIBLE);
            right_button.setBackgroundResource(R.drawable.bg_rect_buy_black);
//            right_button.setText("赠送");
        } else {

            right_button.setTextColor(Color.parseColor("#FFFFFF"));
            if(bottom_btn_type == 0){
                left_button.setVisibility(View.VISIBLE);
                right_button.setVisibility(View.GONE);
                left_button.setBackgroundResource(R.drawable.bg_rect_buy_black);
            }else if(bottom_btn_type == 1){
                left_button.setVisibility(View.GONE);
                right_button.setVisibility(View.VISIBLE);
                right_button.setBackgroundResource(R.drawable.bg_rect_buy_black);
            }else if(bottom_btn_type == 2){
                left_button.setVisibility(View.VISIBLE);
                right_button.setVisibility(View.VISIBLE);
                left_button.setBackgroundResource(R.drawable.bg_rect_buy_black);
                right_button.setBackgroundResource(R.drawable.bg_rect_buy_black);
            }
            if("1".equals(data.getProduct_type())){
                right_button.setText("立即购买");
            }else if("2".equals(data.getProduct_type())){
                right_button.setText("确认");
            }else if("3".equals(data.getProduct_type())){
                right_button.setText("确认");
            }else if("4".equals(data.getProduct_type())){
                right_button.setText("立即购买");
            }

            //券后价按钮展示文案
            float coupon_price_amount = Float.valueOf(data.getAfter_coupon_price());
            if(coupon_price_amount >= 0){
                right_button.setText("领券购买");
            }
        }


    }

    private void changeReducePlus(Integer num) {
        rl_plus.setEnabled(true);
        iv_reduce.setImageResource(R.mipmap.sku_dialog_less_icon);
        iv_plus.setImageResource(R.mipmap.sku_dialog_add_icon);
        if (num <= 1) {
            et_num.setText(1 + "");
            et_num.setSelection(1);
            iv_reduce.setImageResource(R.mipmap.sku_dialog_less_unclick_icon);
            if (!TextUtils.isEmpty(stock) && num >= Long.valueOf(stock)) {
                iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
            }
        }else if (!TextUtils.isEmpty(stock) && num >= Long.valueOf(stock)) {
            et_num.setText("" + stock);
            et_num.setSelection(("" + stock).length());
            iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
        }
    }

    private void initSingleAttribute(CommodityAttribute attribute, FlowLayout flowLayout) {
        for (CommodityAttributeOption option : attribute.getOptions()) {
            TextView tv = (TextView) View.inflate(context, R.layout.tag_sku_unselect, null);
            tv.setText(option.getTitle());
            ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(dp2px(0), dp2px(0), dp2px(10), dp2px(10));
            if(option.getTitle().length() < 4){
                tv.setGravity(Gravity.CENTER);
            }else{
                tv.setGravity(Gravity.LEFT);
            }

            flowLayout.addView(tv, lp);

            //判断属性是否可点击
            boolean canSelect = optionCanSelect(option);
            if(canSelect){
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(options_selected.contains(option)){
                            options_selected.remove(option);
                        }else{
                            removeSelectAttribute(attribute);//删除同一行选中的value
                            options_selected.add(option);
                        }
                        //选中的改变ui重绘
                        for (int i = 0; i < data.getAttributes().size(); i++) {
                            CommodityAttribute temp = data.getAttributes().get(i);
                            flowLayoutList.get(i).removeAllViews();
                            initSingleAttribute(temp, flowLayoutList.get(i));
                        }
                        display_price_layout();
                        display_bottom_btn();
                        changeReducePlus(buyNum);
                    }
                });
                if(options_selected.contains(option)){
                    tv.setBackgroundResource(R.drawable.bg_rect_buy_black);
                    tv.setTextColor(Color.parseColor("#ffffff"));
                }else{
                    tv.setBackgroundResource(R.drawable.bg_rect_tag_black);
                    tv.setTextColor(Color.parseColor("#333333"));
                }
            }else{
                tv.setBackgroundResource(R.drawable.sku_canot_select);
                tv.setTextColor(0xffc2c5cc);
            }
        }
    }

    String image_url = "";
    String stock = "0";
    private void display_price_layout() {
        ChildProductMessage childProductMessage = getOptionSelectedProductMessage(1,options_selected);
        String integral_price = "",line_price = "",price = "";
        String earn_money = "";
        String after_coupon_price = "-1";
        String coupon_desc = "";
        String vip_price = "";
        if(childProductMessage != null){
            //价格
            integral_price = childProductMessage.getIntegral_price();
            line_price = childProductMessage.getLine_price();
            price = childProductMessage.getPrice();
            //会员价
            vip_price = childProductMessage.getVip_price();
            //赚多少
            earn_money = childProductMessage.getCommission();
            //划线价 隐藏
            //库存
            if(!TextUtils.isEmpty(childProductMessage.getStock())){
                stock = childProductMessage.getStock();
            }
            //图片展示
            image_url = childProductMessage.getImage();
            after_coupon_price = childProductMessage.getAfter_coupon_price();
            coupon_desc = childProductMessage.getCoupon_desc();
        }else{
            integral_price = data.getIntegral_price();
            line_price = data.getLine_price();
            price = data.getPrice();
            vip_price = data.getVip_price();
            earn_money = data.getCommission();
            if(!TextUtils.isEmpty(data.getStock())){
                stock = data.getStock();
            }
            image_url = get_image_url_by_selected_option();
            after_coupon_price = "-1";
            coupon_desc = "";
        }

        price_ll.setData(integral_price,price);

        //会员价
        if (data.isShow_vip_price()){
            layout_vip_price.setVisibility(View.VISIBLE);
            tv_vip_price.setText("¥"+vip_price);
        }else {
            layout_vip_price.setVisibility(View.GONE);
        }

        //赚多少
        String product_type = data.getProduct_type();
        if ("4".equals(product_type) || "3".equals(product_type)
                || "2".equals(product_type) || TextUtils.isEmpty(earn_money)
                || Double.valueOf(earn_money) <= 0) {
            earn_txt.setVisibility(View.GONE);
        } else {
            earn_txt.setVisibility(View.GONE);
            earn_txt.setText("赚" + earn_money);
        }
//        if (!TextUtils.isEmpty(integral_price)&&Long.parseLong(integral_price)>0&&
//                !TextUtils.isEmpty(price)&&Float.parseFloat(price)>0) {
//            line_price_txt.setVisibility(View.GONE);
//            integral_line_price.setVisibility(View.VISIBLE);
//            integral_line_price.setText("¥" + line_price);
//            integral_line_price.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
//        }else {
//            integral_line_price.setVisibility(View.GONE);
            if (!"2".equals(product_type)&&!TextUtils.isEmpty(line_price)&&Float.parseFloat(line_price)!=0&&
                    Float.parseFloat(line_price) != Float.parseFloat(price)) {
                line_price_txt.setVisibility(View.VISIBLE);
                line_price_txt.setText("¥" + line_price);
                line_price_txt.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            }else {
                line_price_txt.setVisibility(View.GONE);
            }
//        }


        //翻倍佣金
        if(!TextUtils.isEmpty(data.getDouble_commission()) && Float.valueOf(data.getDouble_commission()) > 0){
            earn_txt.setVisibility(View.GONE);
            ll_double_earn.setVisibility(View.GONE);
            tv_double_earn.setText("赚" + data.getDouble_commission());
            if(childProductMessage != null && !TextUtils.isEmpty(childProductMessage.getDouble_commission()) && Float.valueOf(childProductMessage.getDouble_commission()) > 0){
                tv_double_earn.setText("赚" + childProductMessage.getDouble_commission());
            }
        }

        //库存
        stock_txt.setText("库存：" + stock + "件");

        //已选择属性
        if (options_selected.size() == 0) {
            String title_str = "";
            for(CommodityAttribute attribute : data.getAttributes()){
                title_str += attribute.getLabel() + " ";
            }
            sku_title.setText("请选择 " + title_str);
            sku_selected_txt.setText("");
        } else {
            sku_title.setText("已选择：");
            String sku_txt = "";
            for (CommodityAttributeOption option : options_selected) {
                sku_txt += ("“" + option.getTitle() + "”");
            }
            sku_selected_txt.setText(sku_txt);
        }

        //图片展示
        GlideUtil.displayImage(context, image_url, iv_goods, R.mipmap.defaults);

        //展示券后价
        if(Float.valueOf(after_coupon_price) < 0){
            coupon_price_layout.setVisibility(View.GONE);
            coupon_price_desc.setVisibility(View.GONE);
        }else{
            coupon_price_layout.setVisibility(View.GONE);
            coupon_price.setText("¥" + after_coupon_price);
            coupon_price_desc.setVisibility(View.VISIBLE);
            coupon_price_desc.setText(coupon_desc);
        }

    }

    private String get_image_url_by_selected_option() {
        String image_url = "";
        for (CommodityAttributeOption option : options_selected){
            if(!TextUtils.isEmpty(option.getImage())){
                image_url = option.getImage();
            }
        }
        if(TextUtils.isEmpty(image_url) && data.getImages().size() > 0){
            image_url = data.getImages().get(0);
        }
        return  image_url;
    }

    private boolean optionCanSelect(CommodityAttributeOption option) {
        boolean flag = false;
        if(options_selected.contains(option)){
            //已经被选中的 默认为可选 因为需要反选
            flag = true;
        }else{
            //获取包含已经选中的option的ChildProductMessage
            List<CommodityAttributeOption> options = new ArrayList<>();
            options.add(option);
            for(CommodityAttributeOption selected : options_selected){
                if(!isSameLabel(option,selected)){
                    options.add(selected);
                }
            }
            ChildProductMessage childProductMessage = getOptionSelectedProductMessage(0,options);
            if(childProductMessage != null){
                flag = true;
            }
        }
        return flag;
    }

    //判断当前option与已经选中的是否在一个label
    private boolean isSameLabel(CommodityAttributeOption option, CommodityAttributeOption selected) {
        boolean flag = false;
        for(CommodityAttribute attribute : data.getAttributes()){
            if(attribute.getOptions().contains(option) && attribute.getOptions().contains(selected)){
                flag = true;
            }
        }
        return flag;
    }

    //0模糊包含就可以 1 严格匹配
    private ChildProductMessage getOptionSelectedProductMessage(int type,List<CommodityAttributeOption> options) {
        ChildProductMessage temp = null;
        List<ChildProductMessage> childProductMessages = data.getChild_products();
        for(ChildProductMessage childProductMessage : childProductMessages){
            String sku_code = childProductMessage.getSku_code();
            String [] optionIds = sku_code.split("_");
            int count = 0;
            for(String optionId : optionIds){
                for(CommodityAttributeOption option : options){
                    if(optionId.equals(option.getOption_id())){
                        count ++;
                    }
                }
            }
            if(type == 0 && count == options.size()){
                return childProductMessage;
            }else if(type == 1 && count == optionIds.length){
                return childProductMessage;
            }
        }
        return temp;
    }

    private void removeSelectAttribute(CommodityAttribute attribute) {
        for (CommodityAttributeOption commodityAttributeOption : attribute.getOptions()) {
            CommodityAttributeOption option = null;
            for (CommodityAttributeOption s : options_selected) {
                if (s.getOption_id().equals(commodityAttributeOption.getOption_id())) {
                    option = s;
                }
            }
            if (option != null) {
                options_selected.remove(option);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    @Override
    public void onClick(View v) {
        ChildProductMessage temp;
        switch (v.getId()) {
            case R.id.close_layout:
                dismiss();
                break;
            case R.id.iv_goods:
                ArrayList<String> urlList = new ArrayList<>();
                urlList.add(image_url);
                Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.LOOK_PICTURE_FRAGMENT)
                        .withStringArrayList("urlList", urlList)
                        .withInt("position", 0)
                        .navigation();
                FragmentHostActivity.openFragment(context, fragment);
                break;
            case R.id.rl_reduce:
                if (buyNum == 1) {//至少购买一件商品
                    showToast("至少购买一件");
                } else {
                    buyNum--;
                    et_num.setText(buyNum + "");
                }
                break;
            case R.id.rl_plus:
                //有限购
                if (limitNum > 0) {
                    //1、限购数量>=库存
                    if (limitNum >= Long.valueOf(stock)) {
                        if (buyNum < Long.valueOf(stock)) {
                            buyNum++;
                            et_num.setText(buyNum + "");
                        } else {//已经达到库存上线
                            rl_plus.setEnabled(false);
                            iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
                            showToast("已达到库存上限");
                        }
                    } else {
                        //2、限购数量<库存
                        if (buyNum < limitNum) {
                            buyNum++;
                            et_num.setText(buyNum + "");
                        } else {//已经达到库存上线
                            rl_plus.setEnabled(false);
                            iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
                            showToast("已达到限购上限");
                        }
                    }
                } else {
                    //无限购
                    if (buyNum < Long.valueOf(stock)) {
                        buyNum++;
                        et_num.setText(buyNum + "");
                    } else {//已经达到库存上线
                        rl_plus.setEnabled(false);
                        iv_plus.setImageResource(R.mipmap.sku_dialog_add_unclick_icon);
                        showToast("已达到库存上限");
                    }
                }
                break;
            case R.id.left_button:
                if (isFastClick()) {
                    return ;
                }
                temp = getOptionSelectedProductMessage(1,options_selected);
                if(temp == null | options_selected.size() < data.getAttributes().size()){
                    showToast("请选择商品属性");
                    return;
                }
                if(skuSelectCallBack != null){
                    String sku_txt = "";
                    for (CommodityAttributeOption option : options_selected) {
                        sku_txt += (option.getTitle() + " ");
                    }
                    skuSelectCallBack.onSkuSelect(temp.getProduct_id(),sku_txt,"" + buyNum,0,"0","","");
                }
                break;
            case R.id.right_button:
                if (isFastClick()) {
                    return ;
                }
                temp = getOptionSelectedProductMessage(1,options_selected);
                if(temp == null | options_selected.size() < data.getAttributes().size()){
                    showToast("请选择商品属性");
                    return;
                }
                if(!"已售罄".equals(right_button.getText().toString().trim())){
                    if(skuSelectCallBack != null){
                        String sku_txt = "";
                        for (CommodityAttributeOption option : options_selected) {
                            sku_txt += (option.getTitle() + " ");
                        }
                        if (data.getActivity_type().equals("3")) {
                            skuSelectCallBack.onSkuSelect(temp.getProduct_id(),sku_txt,"" + buyNum,2,temp.getMax_discount_coupon_code(),temp.getS_coupon_id(),temp.getS_coupon_name());

                        } else {
                            skuSelectCallBack.onSkuSelect(temp.getProduct_id(),sku_txt,"" + buyNum,1,temp.getMax_discount_coupon_code(),temp.getS_coupon_id(),temp.getS_coupon_name());

                        }
                    }
                }
                break;
        }
    }

    private static final int MIN_DELAY_TIME = 800;  // 两次点击间隔不能少于800ms
    private static long lastClickTime;
    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }

    public void setSkuSelectCallBack(SkuSelectCallBack skuSelectCallBack) {
        this.skuSelectCallBack = skuSelectCallBack;
    }

    private SkuSelectCallBack skuSelectCallBack;

    public interface SkuSelectCallBack {
        public void onSkuSelect(String product_id, String skuName, String num, int operate,String max_discount_coupon_id,String couponId,String couponName);//operate 0 加入购物车 1 立刻购买 max_discount_coupon_id最大优惠券的id 0代表没有
    }

    public void showToast(String str) {
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }

}
